package com.fosss.binarySortTree;

/**
 * 二叉排序树
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9};

        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }

        binarySortTree.infixOrder();
        System.out.println("---------------");

        /**
         * 测试删除叶子结点
         */
       /* binarySortTree.deleteNode(2);
        binarySortTree.deleteNode(5);
        binarySortTree.deleteNode(9);
        binarySortTree.deleteNode(12);
        binarySortTree.infixOrder();*/

        /**
         * 测试删除有一棵子树的结点
         */
        /*binarySortTree.deleteNode(1);
        binarySortTree.infixOrder();*/

        /**
         * 测试删除有两棵子树的结点
         */
//        binarySortTree.deleteNode(3);
        binarySortTree.deleteNode(7);

        binarySortTree.infixOrder();
    }
}

/**
 * 创建二叉排序树
 */
class BinarySortTree {
    private Node root;

    /**
     * 删除指定结点
     */
    public void deleteNode(int value) {
        if (this.root == null) {
            return;
        }

        Node targetNode = this.searchTargetNode(value);
        if (targetNode == null) {
            return;
        }

        //如果二叉数只有一个结点
        if (this.root.left == null && this.root.right == null) {
            this.root = null;
        }

        Node parentNode = this.searchParentNode(value);
        /**
         * 1.要删除的是叶子结点
         */

        if (targetNode.left == null && targetNode.right == null) {
            if (parentNode.left == targetNode) {
                parentNode.left = null;
            } else {
                parentNode.right = null;
            }
        }
        /**
         * 2.删除有一棵子树的结点
         */

        else if ((targetNode.left != null && targetNode.right == null) || (targetNode.right != null && targetNode.left == null)) {
            if (targetNode.left != null) {
                //！！！！！！！！如果此时的父结点为空，说明只剩下两个结点，此时只需要将root结点指向被删除的结点的子节点
                if (parentNode == null) {
                    this.root = targetNode.left;
                } else if (parentNode.left == targetNode) {
                    parentNode.left = targetNode.left;
                } else {
                    parentNode.right = targetNode.left;
                }
            } else {
                if (parentNode == null) {
                    this.root = targetNode.right;
                } else if (parentNode.left == targetNode) {
                    parentNode.left = targetNode.right;
                } else {
                    parentNode.right = targetNode.right;
                }
            }
        }

        /**
         * 3.删除有两棵子树的结点
         */

        else {
            //先找到要删除的结点的右子节点中最小的值，让该位置置空并将这个值赋给要删除的结点
            Node temp = targetNode;
            //找到
            while (temp.right != null) {
                //到达右子结点
                temp = temp.right;
                while (temp.left != null) {
                    temp = temp.left;
                }
            }
            //先置空
            deleteNode(temp.value);
            //再赋值

            targetNode.value = temp.value;

        }

    }

    /**
     * 查找要删除的结点的父结点
     */
    public Node searchParentNode(int value) {
        if (this.root == null) {
            System.out.println("无父结点");
            return null;
        } else {
            return this.root.searchParentNode(value);
        }
    }


    /**
     * 查找要删除的结点
     */
    public Node searchTargetNode(int value) {
        if (this.root == null) {
            System.out.println("当前二叉排序树为空");
            return null;
        } else {
            return this.root.searchTargetNode(value);
        }
    }

    /**
     * 添加操作
     *
     * @param node
     */
    public void add(Node node) {
        if (this.root == null) {
            this.root = node;
        } else {
            this.root.add(node);
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.root == null) {
            System.out.println("二叉排序树为空，不能遍历");
        } else {
            this.root.infixOrder();
        }
    }

}

/**
 * 创建结点类
 */
class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    /**
     * 查找要删除的结点的父结点
     *
     * @param value 要删除的值
     * @return 返回父结点
     */
    public Node searchParentNode(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else if (this.left != null && (value < this.value)) {
            return this.left.searchParentNode(value);
        } else if (this.right != null && (value >= this.value)) {
            return this.right.searchParentNode(value);
        } else {
            return null;
        }
    }

    /**
     * 找到要删除的目标结点
     *
     * @param value 要删除的值
     * @return 返回要删除的目标结点
     */
    public Node searchTargetNode(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value && this.left != null) {
            //向左查找
            return this.left.searchTargetNode(value);
        } else if (value >= this.value && this.right != null) {
            //向右查找
            return this.right.searchTargetNode(value);
        } else {
            return null;
        }
    }

    /**
     * 添加方法
     */
    public void add(Node node) {
        if (node.value < this.value) {
            //要添加的值比当前值小，则向左继续查找
            if (this.left != null) {
                this.left.add(node);
            } else {
                this.left = node;
            }

        } else {
            //要添加的值比当前值大
            if (this.right != null) {
                this.right.add(node);
            } else {
                this.right = node;
            }

        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);

        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}


















